/**
 * Created by wangx08 on 2015-05-21.
 */

define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    require('global');

    var api_prefix = G_CONFIG["api_prefix"];
    var api_prefix_site = api_prefix + "site/"; //api前缀
    var angular = require("angular");
    require("angular-ui");

    angular.module("app",["ngUI"])
        .controller("ChangePassword",["$scope",function($scope){
            $scope.data = {
                password:"",
                password1:"",
                password2:""
            };
            $scope.btn_enable = true;
            function clearServerError(){
                $("#server_error").hide();
            }
            $scope.$watch("data.password",clearServerError);
            $scope.$watch("data.password1",clearServerError);
            $scope.$watch("data.password2",clearServerError);
            $scope.save = function(invalid){
                if(invalid)return;
                $scope.btn_enable = false;
                $.ajax(
                    {
                        url:api_prefix_site + "change-password",
                        data:$scope.data,
                        type:"post",
                        success:function(data){
                            if(data.retCode == "0"){
                                parent.closeChangePwdDialogSuccess && parent.closeChangePwdDialogSuccess();
                            }
                            else{
                                $("#server_error").text(data.errMsg).show();
                            }
                            $scope.btn_enable = true;
                            $scope.$applyAsync();
                        },
                        error: function () {
                            $("#server_error").text("服务器发生异常").show();
                            $scope.btn_enable = true;
                            $scope.$applyAsync();
                        }
                    }
                );
            };
        }]);
    angular.bootstrap(document,['app']);
});